<template>
  <div style="margin-bottom: 20px">消息详情</div>
  <div class="messageDetail">
    <div class="messageDetailItem" v-for="item in data" :key="item.id">
      <div
        :class="
          loginUserId === item.senderUser.id
            ? 'messageDetailItemContentRight'
            : 'messageDetailItemContentLeft'
        "
      >
        <img :src="item.senderUser.avatar" alt="" class="avatar" />
        <div style="flex: 1">
          <div class="messagebox">{{ item.content }}</div>
          <div class="createAt">{{ item.createAt }}</div>
        </div>
      </div>
    </div>
    <div class="sender">
      <input type="text" v-model="messageContent" placeholder="回复消息" />
      <div @click="send">发送</div>
    </div>
  </div>
</template>
<script setup>
import { getChatRecord, sendMessage } from "@/api/message";

import { useUserStore } from "@/stores/userStore";
import { ElMessage } from "element-plus";
import { ref } from "vue";
import { useRoute } from "vue-router";
const loginUserId = useUserStore().userInfo.id; // 假设当前登录用户的ID为1
const messageContent = ref(""); // 用于存储输入的消息内容
const route = useRoute();
console.log(route.params.id);

const data = ref([]);
const getData = () => {
  getChatRecord({
    currentUserId: loginUserId,
    targetUserId: Number(route.params.id), // 假设目标用户的ID为2
  }).then((res) => {
    data.value = res.data.data;
  });
};
const send = async () => {
  if (!messageContent.value) {
    ElMessage.error("请输入消息内容");
    return;
  }
  await sendMessage({
    currentUserId: loginUserId,
    targetUserId: Number(route.params.id),
    content: messageContent.value,
  });
  messageContent.value = ""; // 清空输入框
  getData(); // 重新获取聊天记录
};
getData();
</script>

<style scoped lang="less">
.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  margin-right: 10px;
}

.messagebox {
  padding: 5px 10px;
  border-radius: 5px;
  background-color: #ec6149;
  color: #fff;
  font-size: 14px;
  position: relative;
  max-width: 50%;
  word-break: break-all;
  justify-self: flex-start;
  line-height: 30px;
  letter-spacing: 1px;

  &:before {
    content: "";
    position: absolute;
    top: 20px;
    left: -10px;
    transform: translateY(-50%);
    border: 5px solid transparent;
    border-right-color: #ec6149;
  }
}
.createAt {
  font-size: 12px;
  color: #969696;
  margin-top: 2px;
}
.messageDetailItem {
  margin-bottom: 20px;
  display: flex;
}
.messageDetailItemContentLeft {
  flex: 1;
  display: flex;
  align-items: flex-start;
}

.messageDetailItemContentRight {
  flex: 1;
  display: flex;
  align-items: flex-start;
  flex-direction: row-reverse;
  img {
    margin-left: 10px;
    margin-right: 0;
  }
  .messagebox {
    justify-self: flex-end;
    &::after {
      content: "";
      position: absolute;
      top: 20px;
      right: -10px;
      transform: translateY(-50%);
      border: 5px solid transparent;
      border-left-color: #ec6149;
    }
    &:before {
      content: "";
      display: none;
    }
  }
  .createAt {
    font-size: 12px;
    color: #969696;
    margin-top: 2px;
    text-align: right;
  }
}
.sender {
  display: flex;
  align-items: center;
  margin-top: 20px;
  input {
    padding: 10px;
    border-radius: 5px;
    border: 1px solid #eee;
    flex: 1;
    margin-right: 10px;
    outline: none;
    font-size: 14px;
    background-color: #fafafa;
  }
  div {
    padding: 10px 20px;
    background-color: #ec6149;
    color: #fff;
    border-radius: 5px;
    cursor: pointer;
  }
}
</style>
